/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.Player;
import models.TradeGood;
import models.Marketplace;
import models.Ship;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.imageio.ImageIO;
import javax.swing.border.LineBorder;

import java.awt.event.ActionListener;

import java.awt.Image;
import java.awt.Dimension;
import java.awt.Color;


import java.io.File;
import java.text.NumberFormat;

/**
 * Trade Good Panel
 * @author twb
 * @version 1.0
 */
public class TradeGoodPanel extends JPanel {
	
	/**
	 * 10
	 */
	private static final int TEN = 10;
	
	/**
	 * 15
	 */
	private static final int FIFTEEN = 15;
	
	/**
	 * 20
	 */
	private static final int TWENTY = 20;
	
	/**
	 * 35
	 */
	private static final int THIRTYFIVE = 35;
	
	/**
	 * 40
	 */
	private static final int FORTY = 40;
	
	/**
	 * 50
	 */
	private static final int FIFTY = 50;
	
	/**
	 * field
	 */
	private final TradeGood tradegood;

	/**
	 * field
	 */
	private final Player player;

	/**
	 * field
	 */
	private JLabel tradeGoodIcon;
    
	/**
	 * field
	 */
	private final JLabel tradeGoodField;
    
	/**
	 * field
	 */
	private final JLabel inMarketField;
    
	/**
	 * field
	 */
	private final JButton sellButton;
    
	/**
	 * field
	 */
	private final JLabel priceField;
    
	/**
	 * field
	 */
	private final JButton buyButton;
    
	/**
	 * field
	 */
	private final JLabel onShipField;

    /**
     * Constructor for TradeGoodPanel.
     * @param player Player
     * @param good TradeGood
     */
    public TradeGoodPanel(Player player, TradeGood good) {
        super.setLayout(null);     
        super.setBorder(new LineBorder(Color.BLACK));
        super.setBackground(Color.WHITE);

        this.player = player;
        this.tradegood = good;

        try {
        	final File imageFile = new File("resources/" + good.getName() + "Icon.png");
        	if (imageFile != null) {
        		final Image tradeGoodImage = ImageIO.read(imageFile).getScaledInstance(
        				THIRTYFIVE, THIRTYFIVE, Image.SCALE_DEFAULT);
        		tradeGoodIcon = new JLabel(new ImageIcon(tradeGoodImage));
        	} else {
        		tradeGoodIcon = new JLabel();
        	}
        	
        } catch (Exception e) {
            tradeGoodIcon = new JLabel(); 
			System.err.println(e);
        }

        tradeGoodField = new JLabel("  " + good.getName());
        //tradeGoodIcon.setBorder(new LineBorder(Color.BLACK));
        
        buyButton = new JButton("Buy");
        sellButton = new JButton("Sell");
        
        inMarketField = new JLabel();
        onShipField = new JLabel();
        priceField = new JLabel();

        super.add(tradeGoodIcon);
        super.add(tradeGoodField);
        super.add(buyButton);
        super.add(sellButton);
        super.add(inMarketField);
        super.add(onShipField);
        super.add(priceField);
        
        update();
    }

    /**
     * Method update.
     */
    public final void update() {
        final Marketplace marketplace = player.getLocation().getMarketplace();
        final Ship ship = player.getShip();
        final NumberFormat fmt = NumberFormat.getCurrencyInstance();
        
    	inMarketField.setText("" + marketplace.getQuantity(tradegood));
    	priceField.setText(fmt.format(marketplace.getPrice(tradegood)));
    	onShipField.setText("" + ship.getTradeGoodQuantity(tradegood));
        
    }

    /**
     * Method layoutSubviews.
     */
    public void layoutSubviews() {
        final Dimension size = getSize();

        int x = 0;
        
        tradeGoodIcon.setBounds(0, 0, size.height, size.height);
        x += size.height;

        tradeGoodField.setBounds(x, 0, size.height << 1, size.height); 
        x += size.height << 1;

        inMarketField.setBounds(x, 0, size.height, size.height);
        x += size.height;

        priceField.setBounds(x, 0, size.height + FIFTY, size.height);
        x += size.height + FORTY;

        onShipField.setBounds(x, 0, size.height, size.height);
        x += size.height;

        x = size.width;

        sellButton.setBounds(x - size.height - FIFTEEN, 0, 
        		size.height + FIFTEEN, size.height);
        
        x -= size.height + TEN;

        buyButton.setBounds(x - size.height - TWENTY, 0, 
        		size.height + FIFTEEN, size.height);
        
        x -= size.height + TEN;
    }

    /**
     * Method getTradeGood.
     * @return TradeGood
     */
    public TradeGood getTradeGood() {
        return tradegood;
    }

    /**
     * Method addActionListener.
     * @param listener ActionListener
     */
    public void addActionListener(ActionListener listener) {
    	if (buyButton != null) {
    		buyButton.addActionListener(listener);
    	}
    	
    	if (sellButton != null) {
    		sellButton.addActionListener(listener);
    	}
    }
    
    /**
     * Method getBuyButton.
     * @return JButton
     */
    public JButton getBuyButton() {
    	return buyButton;
    }
    
    /**
     * Method getSellButton.
     * @return JButton
     */
    public JButton getSellButton() {
    	return sellButton;
    }

    /**
     * Method setBounds.
     * @param x int
     * @param y int
     * @param width int
     * @param height int
     */
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        layoutSubviews();
    }
}
